package com.ninth_group.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ninth_group.dto.CityDTO;
import com.ninth_group.exception.city.CityException;
import com.ninth_group.result.R;
import com.ninth_group.service.Forecast7DService;
import com.ninth_group.vo.Forecast7DVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 城市7天天气信息
 */
@RestController
@RequestMapping("/7d")
public class Forecast7DController {
    @Resource
    private Forecast7DService forecast7DService;

    /**
     * 获取城市7天天气信息
     * @param cityDTO 城市信息
     * @return 7天天气信息
     */
    @PostMapping("/c")
    public R getForecast7dcData(@RequestBody CityDTO cityDTO) throws JsonProcessingException {
        List<Forecast7DVO> city7DaysData = forecast7DService.getCity7dcData(cityDTO);
        return R.ok("success", city7DaysData);
    }

    /**
     * 获取省份(省会)7天天气信息
     * @param pName 省份名称
     * @return 省会7天天气信息
     */
    @GetMapping("/p/{pName}")
    public R getForecast7dpData(@PathVariable String pName) throws JsonProcessingException {
        System.out.println("pName = " + pName);
        List<Forecast7DVO> city7DaysData = forecast7DService.getCity7dpData(pName);
        city7DaysData.forEach(System.out::println);
        return R.ok("success", city7DaysData);
    }

    @ExceptionHandler(CityException.class)
    public R handleCityException(CityException e) {
        return R.error(e.getMessage());
    }
}
